let rawcoco = require('./valid.json')
let fs = require('fs')
let path = require('path')
let offset = 0
for (let i = 0; i < rawcoco.images.length; i++) {
    imagepath = rawcoco.images[i].file_name
    imageid = rawcoco.images[i].id
    let temp = ""
    for (; offset < rawcoco.annotations.length && rawcoco.annotations[offset].image_id == imageid; offset++) {
        temp += "" + rawcoco.annotations[offset].category_id
        let xmin = rawcoco.annotations[offset]["bbox"][0]
        let ymin = rawcoco.annotations[offset]["bbox"][1]
        let xmax = rawcoco.annotations[offset]["bbox"][2] + rawcoco.annotations[offset]["bbox"][0]
        let ymax = rawcoco.annotations[offset]["bbox"][3] + rawcoco.annotations[offset]["bbox"][1]
        // console.log(xmin,xmax,ymin,ymax)
        temp += " " + ((xmax + xmin) / 2 / rawcoco.images[i].width).toFixed(6)
        temp += " " + ((ymax + ymin) / 2 / rawcoco.images[i].height).toFixed(6)
        temp += " " + (Math.abs(rawcoco.annotations[offset]["bbox"][2]) / rawcoco.images[i].width).toFixed(6);
        temp += " " + (Math.abs(rawcoco.annotations[offset]["bbox"][3]) / rawcoco.images[i].height).toFixed(6);
        temp += "\n"
    }
    // console.log(temp)
    fs.writeFileSync(path.resolve(__dirname, "val", imagepath.substr(0, imagepath.length - 4) + ".txt"), temp)
}

